//
//  MyCarsViewController.m
//  SITABLE
//
//  Created by songjun on 16/9/19.
//  Copyright © 2016年 Sita. All rights reserved.
//

#import "MyCarsViewController.h"

@interface MyCarsViewController ()

@end

@implementation MyCarsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    bikeCodeArr = [NSMutableArray arrayWithCapacity:1];
    vinPwdArr = [NSMutableArray arrayWithCapacity:1];
    
    [bikeCodeArr addObject:@"车架号: 20160101"];
    [bikeCodeArr addObject:@"车架号: 20160102"];
    [bikeCodeArr addObject:@"车架号: 20160103"];
    
    [vinPwdArr addObject:@"车辆型号: 新大洲2016款I"];
    [vinPwdArr addObject:@"车辆型号: 新大洲2016款II"];
    [vinPwdArr addObject:@"车辆型号: 新大洲2016款III"];
    
    self.title = NSLocalizedStringFromTable(@"Mycars", @"InfoPlist", nil);
    if ([baseTableview respondsToSelector:@selector(setSeparatorInset:)]) {
        [baseTableview setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([baseTableview respondsToSelector:@selector(setLayoutMargins:)]) {
        [baseTableview setLayoutMargins:UIEdgeInsetsZero];
    }
    [baseTableview setSeparatorColor:[UIColor clearColor]];
    self.view.backgroundColor =UIColorHexRGBA(linBoardBGGrayColor, 1.0);
    baseTableview.backgroundColor = UIColorHexRGBA(linBoardBGGrayColor, 1.0);
    
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0)];
    [baseTableview setTableFooterView:footerView];
    
//    UIButton *addBikeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    addBikeBtn.frame = CGRectMake(0, 0, 40, 40);
//    [addBikeBtn setTitle:@"添加" forState:UIControlStateNormal];
//    addBikeBtn.titleLabel.font = linBoardFont15;
//    [addBikeBtn addTarget:self action:@selector(addBikeBtnAction:) forControlEvents:UIControlEventTouchUpInside];
//    [addBikeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//
//    UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc] initWithCustomView:addBikeBtn];
//    self.navigationItem.rightBarButtonItem = rightBarBtn;
    
    
}

-(void)addBikeBtnAction:(UIButton *)btn
{
    BindingBikeViewController *bikeDetect =  [[UIStoryboard storyboardWithName:@"Tab3" bundle:nil] instantiateViewControllerWithIdentifier:@"bindingBikeVc"];
    UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
    temporaryBarButtonItem.title = @"";
    self.navigationItem.backBarButtonItem = temporaryBarButtonItem;

    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:bikeDetect animated:YES];
    //self.hidesBottomBarWhenPushed = NO;
    
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(nonnull UITableViewCell *)cell forRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    // 某一行分割线左对齐
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return bikeCodeArr.count;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *hearderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
    hearderView.backgroundColor = UIColorHexRGBA(linBoardBGGrayColor, 1.0);
    return hearderView;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identify = @"cell";
    MyCarsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MyCarsTableViewCell" owner:self options:nil] lastObject];
    }
    cell.bikeCodeLable.text = [bikeCodeArr objectAtIndex:indexPath.section];
    cell.hiddenLabel.text =[bikeCodeArr objectAtIndex:indexPath.section];
    //cell.carTypeLabel.text = [vinPwdArr objectAtIndex:indexPath.row];
    [cell.unBindCar addTarget:self action:@selector(unBindingCarAciton:) forControlEvents:UIControlEventTouchUpInside];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    NSString *changeString = [[NSUserDefaults standardUserDefaults] valueForKey:@"pallaCurrentBindBikeCode"];
    if ([cell.hiddenLabel.text isEqualToString:changeString]) {
        cell.changeCarBtn.hidden = YES;
        cell.currentCarLabel.hidden = NO;
    }
    
    [cell.changeCarBtn addTarget:self action:@selector(changeBikeAction:) forControlEvents:UIControlEventTouchUpInside];
    
    return cell;
}

-(void)changeBikeAction:(UIButton *)btn
{
    MyCarsTableViewCell *cell = (MyCarsTableViewCell *)btn.superview.superview;
    changeBikeCode = cell.hiddenLabel.text;
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"车辆切换" message:@"确定要切换这部车?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alertView.tag = 1002;
    [alertView show];
    
    
    
}

-(void)unBindingCarAciton:(UIButton *)btn
{
    MyCarsTableViewCell *cell = (MyCarsTableViewCell *)btn.superview.superview;
    selectedBikeCode = cell.hiddenLabel.text;
    
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"解绑车辆" message:@"确定要解绑这部车?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alertView.tag = 1001;
    [alertView show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    if (alertView.tag == 1001) {
        if (buttonIndex==1) {
            //NSLog(@"11111");
            for (int i =0; i<bikeCodeArr.count; i++) {
                NSString *bikeString = [bikeCodeArr objectAtIndex:i];
                if ([bikeString isEqualToString:selectedBikeCode]) {
                    [bikeCodeArr removeObject:bikeString];
                }
            }
            [baseTableview reloadData];
        }
    }
    
    if (alertView.tag == 1002) {
        if (buttonIndex==1) {
        [[NSUserDefaults standardUserDefaults] setValue:changeBikeCode forKey:@"pallaCurrentBindBikeCode"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [baseTableview reloadData];
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


- (IBAction)addCarBtnAciton:(UIButton *)sender
{
    ScanBleViewController *peripheralVc =   [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"scanBleVc"];
    UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
    temporaryBarButtonItem.title = @"";
    self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
    
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:peripheralVc animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}
@end
